package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Category;
import com.itheima.entity.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    SetmealService setmealService;

    @Autowired
    CategoryService categoryService;

    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDto.categoryId")
    public R<String> save(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithDish(setmealDto);

        return R.success("添加成功");
    }

    @GetMapping("page")
    public R<Page<SetmealDto>> page(Integer page,Integer pageSize,String name){
        Page<Setmeal> pageInfo1 = new Page<>(page, pageSize);

        LambdaQueryWrapper<Setmeal> queryWrapper = Wrappers.lambdaQuery(Setmeal.class)
                .like(StringUtils.isNotBlank(name), Setmeal::getName, name)
                .orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo1,queryWrapper);

        Page<SetmealDto> pageInfo2 = new Page<>();
        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");

        List<SetmealDto> setmealDtoList = new ArrayList<>();

        for (Setmeal setmeal : pageInfo1.getRecords()) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            setmealDtoList.add(setmealDto);
        }
        pageInfo2.setRecords(setmealDtoList);

        return R.success(pageInfo2);
    }
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public R<String> delete(Long[] ids) {
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }

    @GetMapping("{id}")
    @Cacheable(cacheNames = "setmealCache",key = "#id")
    public R<SetmealDto> getById(@PathVariable Long id) {
       SetmealDto setmealDto= setmealService.getWithDish(id);

       return R.success(setmealDto);
    }

    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    @GetMapping("list")
    @Cacheable(cacheNames = "setmealCache",key = "#setmeal.categoryId",unless = "#result.data.size()==0")
    public R<List<Setmeal>> list(Setmeal setmeal){

        LambdaQueryWrapper<Setmeal> queryWrapper = Wrappers.lambdaQuery(Setmeal.class)
                .eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId, setmeal.getCategoryId())
                .eq(Setmeal::getStatus, 1)
                .orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
